﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace XYX.Http.Mvc
{
    /// <summary>
    /// 方法描述缓存
    /// </summary>
    public class ActionDescriptorCache
    {
        //缓存集合
        private static readonly ConcurrentDictionary<MethodInfo, ActionDescriptor> cache;
        /// <summary>
        /// 构造
        /// </summary>
        static ActionDescriptorCache()
        {

            cache = new ConcurrentDictionary<MethodInfo, ActionDescriptor>();
        }
        /// <summary>
        /// 获取描述
        /// </summary>
        /// <param name="method"></param>
        /// <returns></returns>
        public static ActionDescriptor GetActionDescriptor(MethodInfo method)
        {
            return cache.GetOrAdd(method, GetDescriptor);
        }
        /// <summary>
        /// 获取描述
        /// </summary>
        /// <param name="method"></param>
        /// <returns></returns>
        private static ActionDescriptor GetDescriptor(MethodInfo method)
        {

            IActionAttribute [] attributes = (ActionAttribute [])method.GetCustomAttributes(typeof(IActionAttribute)).ToArray();

            var actionName = method.Name;

            ActionDescriptor actionDescriptor = new ActionDescriptor()
            {
                Member = method,
                Name = actionName,
                Attributes = attributes
            };
            return actionDescriptor;

        }
    }
}
